package com.bx.implatform.util;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class VideoCoverUtil {

    /**
     * @throws
     * @Title: getTempPath
     * @Description: 生成视频的首帧图片方法
     * @author: Zing
     * @param: @param tempPath 生成首帧图片的文件地址
     * @param: @param filePath 传进来的线上文件
     * @param: @return
     * @param: @throws Exception
     * @return: boolean
     */
    public static byte[] getCoverAsBytes(byte[] videoBytes) {
        try {
            // 创建临时文件
            File tempFile = File.createTempFile("video-", ".tmp");
            tempFile.deleteOnExit(); // 程序退出时自动删除

            // 将字节流写入临时文件
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                fos.write(videoBytes);
            }

            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(tempFile);
            ff.start();
            int ftp = ff.getLengthInFrames();
            int flag = 0;
            Frame frame = null;
            while (flag <= ftp) {
                // 获取帧
                frame = ff.grabImage();
                // 过滤前3帧，避免出现全黑图片
                if ((flag > 3) && (frame != null)) {
                    break;
                }
                flag++;
            }
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(FrameToBufferedImage(frame), "jpg", byteArrayOutputStream);

            ff.close();
            ff.stop();

            return byteArrayOutputStream.toByteArray();

        } catch (Exception e) {
            throw new RuntimeException("Failed to get cover as bytes", e);
        }
    }

    /***
     *
     * @Title: FrameToBufferedImage
     * @Description: 创建格式化BufferedImage对象
     * @author: Zing
     * @param: @param frame
     * @param: @return
     * @return: RenderedImage
     * @throws
     */
    private static RenderedImage FrameToBufferedImage(Frame frame) {
        // 创建BufferedImage对象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }

    /**
     * @throws
     * @Title: isVideo
     * @Description:判断是不是视频
     * @author: Zing
     * @param: @param path 文件路径
     * @param: @return
     * @return: boolean       true是视频 false非视频
     */
    public static boolean isVideo(String path) {
        // 设置视频后缀
        List<String> typeList = new ArrayList<String>();
        typeList.add("mp4");
        typeList.add("flv");
        typeList.add("avi");
        typeList.add("rmvb");
        typeList.add("rm");
        typeList.add("wmv");
        // 获取文件名和后缀
        String suffix = path.substring(path.lastIndexOf(".") + 1);
        for (String type : typeList) {
            // 统一为大写作比较
            if (type.toUpperCase().equals(suffix.toUpperCase())) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) throws Exception {
        // boolean tempPath = VideoCoverUtil.getTempPath("E:/temporary/542.jpg", "C:/Users/92339/Desktop/0.mp4");
        // System.out.println("获取首帧是否成功！" + tempPath);
    }

}
